package com.xy.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xy.springcloud.pojo.Dept;
import com.xy.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;


    @GetMapping("/dept/queryById/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")//HyStrix处理雪崩，出错会去找指定的方法
    public Dept queryById(@PathVariable Long id){
        Dept dept = deptService.queryByid(id);
        if(dept==null){
            throw new RuntimeException("id===>"+id+",不存在该用户，信息无法找到！");
        }
        return dept;
    }

    public Dept hystrixGet(@PathVariable Long id){
        return new Dept()
                .setDeptno(id)
                .setDname("id===>"+id+",没有对应的信息，null--@Hystrix")
                .setDbSource("Not this database in MySql");
    }
}
